﻿using System;
using UnityEngine;

namespace Core
{
    // ReSharper disable  InvalidXmlDocComment

    /// <summary>
    /// 若要使用该类型作为Dict的key，则需要实现IComparable, IComparable<WH>,接口
    /// </summary>

    // ReSharper disable once InconsistentNaming
    public struct WH : IEquatable<WH>
    {
        public static readonly WH Invalid = Parse(float.Epsilon, float.Epsilon);

        public float Width;
        public float Height;

        public readonly Vector2 Center => new(Width * 0.5f, Height * 0.5f);

        public WH(float w, float h)
        {
            Width = w;
            Height = h;
        }

        public static WH Parse(float w, float h)
        {
            WH obj;
            obj.Width = w;
            obj.Height = h;
            return obj;
        }

        public static WH operator +(WH point, WH vec)
        {
            WH obj;
            obj.Width = point.Width + vec.Width;
            obj.Height = point.Height + vec.Height;
            return obj;
        }

        public static WH operator *(WH point, float value)
        {
            WH obj;
            obj.Width = point.Width * value;
            obj.Height = point.Height * value;
            return obj;
        }

        public static WH operator -(WH point, WH vec)
        {
            WH obj;
            obj.Width = point.Width - vec.Width;
            obj.Height = point.Height - vec.Height;
            return obj;
        }

        public bool Equals(WH other) => Mathf.Approximately(Width, other.Width) && Mathf.Approximately(Height, other.Height);

        public override bool Equals(object obj)
        {
            if (obj == null || (obj is WH) == false)
            {
                return false;
            }
            return Equals((WH)obj);
        }

        public override int GetHashCode()
        {
            return Width.GetHashCode() ^ (Height.GetHashCode() << 2);
        }

        public override string ToString()
        {
            return $"({Width},{Height})";
        }

        public Vector2 ToVec()
        {
            return new Vector2(Width, Height);
        }
    }
}

